package cn.edu.nju.software.fantasyCard.network.logic;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import cn.edu.nju.software.fantasyCard.bean.User;
import cn.edu.nju.software.fantasyCard.screens.LogInScreen;

public class LoginHandle extends IoHandlerAdapter{
	private LogInScreen logInScreen;
	private User user = null;
	
	public LoginHandle(User user){
		this.user = user;
	}

	public LoginHandle(LogInScreen logInScreen){
		this.logInScreen = logInScreen;
	}
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)throws Exception {
		super.exceptionCaught(session, cause);
	}

	//当服务器端的消息到达时
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		super.messageReceived(session, message);
		String reply = (String)message;
		System.out.println("服务器端返回的消息为：" + reply);
//		if(reply.equals("OK")) {
//			logInScreen.logInSuccess();
//		}
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		super.sessionIdle(session, status);
	}

	//当一个客端端连结到服务器后
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
		User user = new User();
		user.setName("1");
		user.setPassword("1");
		System.out.println("user " + user.getName()+ "连接到服务器");
		session.write(user);
	}

}
